<?php
/** Patient
	* 
	* @package Pharmacy Intervention 
	* @subpackage Controller
	* @category Administrative 
	* @author Ray Nowell 
	*/  
class Facility extends MY_Controller {

	function Facility()
	{
		parent::MY_Controller(MUST_BE_ADMIN);
	}
	
	//Private Functions:
	function _get_form_post_data($add_record = false)
	{
		$this->load->helper('date');

		$data = array(
						'facility_id' => $this->input->post('facility_id'),
						'facility_name'=>$this->input->post('facility_name'),
						'facility_site_url'=>$this->input->post('facility_site_url'),
						'active'=>$this->input->post('active'),
						'modified_date'	=>	date("Y-m-d H:i:s"),
						'modified_by'	=>	$this->ion_auth->get_user()->username,
		);
    
    if ($add_record)
      array_shift($data);
		
		return $data;
	}
	
	function _get_additional_javascript()
	{
		$ret_val = '$("#_facilityName").focus();';
		return $ret_val; 
	}
	
	function add()
	{
		$this->load->helper('form');
		$this->load->helper('date');
		$this->load->model('Facility_model');

		$data['title'] = COMPANY_NAME." -- Add Facility ";
		$data['heading'] = "Facility Form";
	
		//Set the values for the Facility View
		$data['facility_id'] = '';
		$data['facility_name'] = '';
		$data['facility_site_url'] = '';
		$data['active'] = '1';	
		$data['created_date'] = '';
		$data['modified_date'] = '';

		$data['save_type'] = 'save_new';
		$data['additional_javascript_doc_ready_functions'] = $this->_get_additional_javascript();
						
		$this->load->view('facility_view', $data);	 
		
	}
	 
	function edit()
	{
		$this->load->helper('date');
		$this->load->model('Facility_model');
			
		//$this->output->enable_profiler(TRUE); 

		$data['title'] = COMPANY_NAME." -- Facility Edit";
		$data['heading'] = "Facility Edit";
		
			//The number of segments will determine the edit mode of the intervention:	single-list or multi-record navigation
		$total_segments = $this->uri->total_segments();
		
		if ($total_segments == SINGLE_EDIT)
		{			
			$facility_id = $this->uri->segment(3, 0);	//Three is the Intervention Id.	zero is the default value
			$data['additional_javascript_doc_ready_functions'] = $this->_get_additional_javascript().get_save_javascript('facility');
		}
		else 
		{
			$segment_to_get = $this->uri->segment(3);

			$facility_id = $this->uri->segment($segment_to_get);
			
			$previous_segment = $segment_to_get <= 4 ? 4 : $segment_to_get - 1;
			$next_segment = $segment_to_get == $total_segments	? $total_segments : $segment_to_get + 1;
			$fullUri = $this->uri->uri_string();
			$search_text = $this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3);
			$replace_text = $this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'|';
			$current_uri = str_replace($search_text, $replace_text, $fullUri);
			$nav_url = base_url().'index.php'.$current_uri;
			$data['additional_javascript_doc_ready_functions'] = get_navigation_doc_ready_javascript().$this->_get_additional_javascript().get_save_javascript('facility');
			$data['additional_jquery_javascript'] = get_navigation_functions($nav_url, $previous_segment, $next_segment, $total_segments);
			$data['nav_bar'] = get_navigation_markup(base_url(), $segment_to_get - 3, $total_segments - 3);
			
		}

		$facility_row = $this->Facility_model->get_facility_id_record($facility_id);
		
		//Set the values for the Facility View
		$data['facility_id']		=	$facility_row->facility_id;
		$data['facility_name']		=	$facility_row->facility_name; 
		$data['facility_site_url']	=	$facility_row->facility_site_url;
		$active_checkbox = array(
					    'name'        => 'active',
					    'id'          => '_active',
					    'value'       => '1',
					    'checked'     => $facility_row->active,
		    		);
		$data['active_checkbox']	=	$active_checkbox;
		$data['created_date']		=	$facility_row->created_date;
		$data['modified_date']		=	$facility_row->modified_date;

		$data['save_type'] = 'update';
		
		$this->load->view('facility_view', $data);	 
		
	}
	
	/** 
	 * Calls the Facility Model to save the patient based on the post data passed
	 * @access public */
	function save_new()
	{
		$data = $this->_get_form_post_data(true);
		
		$data['created_date'] = date('Y-m-d H:i:s');
		$data['created_by'] = $this->ion_auth->get_user()->username;
		
		$this->Facility_model->Add_facility($data);
		redirect('facility/add');
	}
	
	/** 
	 * Calls the Facility Model to update the facility based on the post data passed
	 * @access public */
	function update()
	{
		$data = $this->_get_form_post_data();
		$this->Facility_model->Update_facility($data);
		redirect('facility/edit/'.element('facility_id', $data));
	}

}

/* End of file facility.php */
/* Location: ./system/application/controllers/facility.php */
